﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using TraiderInformationUI.Interfaces.Proxy;
using TraiderInformationUI.ServiceProxy.SecurityService;

namespace TraiderInformationUI.ServiceProxy
{
    [Export(typeof(ISecurityServiceProxy))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class SecurityServiceProxy : ISecurityServiceProxy
    {
        private readonly SecurityServiceClient _client;

        public SecurityServiceProxy()
        {
            _client = new SecurityServiceClient();
        }
        
        public void Register(string login, string email, string password)
        {
            _client.Register(new RegisterRequest
                {
                    Login = login,
                    Email = email,
                    Password = password,
                    Roles = new[]
                        {
                            RoleType.User
                        }
                });
        }

        public void Register(string login, string email, string password, IEnumerable<Models.Models.Security.RoleType> assignedRoles)
        {
            
            _client.Register(new RegisterRequest
            {
                Login = login,
                Email = email,
                Password = password,
                Roles = ConvertRoles(assignedRoles)
            });
        }

        private static RoleType[] ConvertRoles(IEnumerable<Models.Models.Security.RoleType> roles)
        {
            return roles.Select(x =>
            {
                RoleType role;
                Enum.TryParse(x.ToString(), out role);
                return role;

            }).ToArray();
        }
    }
}
